$(function() {
	var bussinessId = $("#identify").val();

	initPage(bussinessId);
	
	$("body").on("click", ".extendimg", function (){
        var id=$(this).attr('id');
        	jQuery("#avatarbig").prop("src",$("#"+id+"_img").attr("src"));
	});

});
// 展示详细信息
$("body").on("click",".hrefClick",function() {
	$("#inspectEnvir").text("");
	$("#xmxqList").empty();
	$("#xmxqList").append('<li data-role="list-divider" class="list-divider-ui">项目详情</li>');
	// 初始化面板信息
	var inspectId = $(this).attr("defineId");// 自定义的属性,方便id的传递

	$.ajax({
			url : window.rootPath
					+ '/ui/bussinessInspection/findById/'
					+ inspectId,
		
			dataType : 'json',
			type : 'get',
			success : function(result) {
				if (result.code == 1) {
					var info = result.data.info;
					// 构建面板数据
					$("#bussinessName").html(info.businesssellerBaseInfo.shopName);
					$("#bussiessAddr").html(info.businesssellerBaseInfo.shopAdress);
					$("#bussinessMng").html(info.businesssellerBaseInfo.managementPerson.name);
					$("#remark").html(info.remark);
					var list=info.details;
					var enr='';
					for(var i=0;i<list.length;i++){
						var purl=list[i].picAddr;
						if (purl == undefined || purl == "") {
							purl = "img/nopicture.png";
						}
						
//						var  contenthtml ='<tr><td colspan="2" style="color: #999999; border-bottom: 2px solid #55B937 !important;"></td></tr>'
//						  +'<tr><td width="1%"><p style="color: #999999;">现场图片</p></td><td><a href="#pop" data-rel="popup" data-position-to="window" id="'+list[i].id+'" class="extendimg" data-transition="fade"><img id="'+list[i].id+'_img" width="30%" src="'+purl+'"/></a></td></tr>'  
//						  +'<tr><td><p style="color: #999999;">备注</p></td><td><p>'+list[i].remark+'</p></td></tr>' 
//		                  +'<tr><td><p style="color: #999999;">时间</p></td><td><p>'+moment(list[i].createTime).format('YYYY-MM-DD HH:mm')+'</p></td></tr>';
//		                  $("#xmxqList").append(contenthtml);
		                  
		                var infoId = list[i].id;
		                var memo1 = "备注:"+list[i].remark;
		                var memo2 = "时间:"+moment(list[i].createTime).format('YYYY-MM-DD HH:mm');
		                $("#xmxqList").setListViewContent(infoId,purl,memo1,memo2);
		                
					}
					//	$("#inspectEnvir").append(enr);
					// $("#inspectEnvirResult").html(info.environmentOpt.title);
					$("#inspectCondition").html(info.inspectCondition.title);
					
					$("#inspectDate").html(moment(info.inspectDate).format('YYYY-MM-DD HH:mm'));
					$("#inspectPerson").html(info.user.name);
					
					var imgUrl=info.pictureAddr;
					if(imgUrl==undefined||imgUrl==""){
						imgUrl="../img/nopicture.png";
					}else{
						imgUrl=window.rootPath+'/'+imgUrl;
					}
					$("#avatartI").attr("src",imgUrl);
					
				}
			}
		});
	// 指向面板属性的id
	$(this).prop("href", "#detailInspectPage");

});
function initPage(bussinessId) {
	// $("#inspectUl").empty();
	$.ajax({
		url : window.rootPath
				+ '/ui/bussinessInspection/pageOnPhone?device=mobile&identify='
				+ bussinessId,
		dataType : 'json',
		type : 'get',
		success : function(result) {
			var uncheckedInfo = result.data.uncheckedInfo;
			var checkedInfo = result.data.checkedInfo;

			if (result.code == 1) {
				// 构建巡检记录
				// buildUncheckedInspection(uncheckedInfo);
				// 构建巡检历史
				// buildUncheckedInspection(checkedInfo);

				buildInfo(uncheckedInfo, "#unCheckedInspections");
				buildInfo(checkedInfo, "#checkedInspections");

			}
		}
	});
};

/*
function buildInfo(info, str) {

	if (info.length == 0) {
		var contenthtml = '<li><h2 style="color:blue">暂无巡检信息</h2></li>';
		$(str).append(contenthtml);
		$(str).listview('refresh');
	}

	for (var i = 0; i < info.length; i++) {

		var purl = info[i].pictureAddr;// 没有上传图片

		if (purl == undefined || purl == "") {
			purl = "img/nopicture.png";
		}
		purl=rootPath+"/"+purl;
		var title = info[i].inspectCondition.title;
		var contenthtml = '<li><a defineId=' + info[i].id
				+ ' class="hrefClick"><img width="80%" src="' +purl
				+ '" /><h2 >总体评价:' + title + '</h2>'
				+ '<p style="color:blue">时间：'
				+ moment(info[i].inspectDate).format('YYYY-MM-DD HH:mm') + '</p>'
				+ '</a>' + '</li>';

		$(str).append(contenthtml);
	}
	$(str).listview('refresh');
};
*/

function buildInfo(pageInfo, str) {
	if (pageInfo.length == 0) {
		var contenthtml = '<li><h2 style="color:blue">暂无巡检信息</h2></li>';
		$(str).append(contenthtml);
		$(str).listview('refresh');
	}
	
	for (var i = 0; i < pageInfo.length; i++) {
//		var contenthtml='<br/><div class="div1">'
//			+'<a href="#detailInspectPanel" data-transition="slide" class="hrefClick" defineId="'+pageInfo[i].id+'" id="'+pageInfo[i].id+'"><div class="div-time"><div style="margin-left:20px;">'
//			+moment(pageInfo[i].inspectDate).format('YYYY-MM-DD HH:mm')
//			+'</div></div></a><br/>'
//		    +'<div class="div-type">总体评价:'+pageInfo[i].inspectCondition.title+'</div><br/>';
//			if(pageInfo[i].details!=null){
//				for(var j=0;j<pageInfo[i].details.length;j++){
//					var temp =pageInfo[i].details[j].inspectOpt.title;
//					if(temp.length>10){
//						temp =temp.substring(0,12)+"...";
//					}
//					if(j<pageInfo[i].details.length-1){
//						contenthtml+='<div class="div-type">'+temp+'</div><br/>';
//					}else{
//						contenthtml+='<div class="div-type">'+temp+'</div>';
//					}
//				}
//			}else{
//				var temp =pageInfo[i].remark;
//				if(temp.length>10){
//					temp =temp.substring(0,12)+"...";
//				}
//				contenthtml+='<div class="div-type">'+temp+'</div>';
//			}
//		contenthtml+='<div style="overflow: auto;padding-top:0;margin-left:20px;">'
//		+'<div class="div-inline"><p style="margin:0;">巡检人:'+pageInfo[i].user.name+'</p></div>'
//		+'<div class="div-inline"><p style="margin:0;">电话:'+pageInfo[i].user.contactType+'</p></div></div>'
//		+'</div>';
//		$(str).append(contenthtml);
		
		var tempHtml ='';
		if(pageInfo[i].details!=null){
			for(var j=0;j<pageInfo[i].details.length;j++){
				var temp = pageInfo[i].details[j].inspectOpt.title;
				if(j<pageInfo[i].details.length-1){
					tempHtml += temp +'、';
				}else{
					tempHtml += temp;
				}
			}
		}
		var purl = pageInfo[i].pictureAddr;// 没有上传图片
		if (purl == undefined || purl == "") {
			purl = "img/nopicture.png";
		}
		
		var listView = $(str);
		var infoId = pageInfo[i].id;
		var pic = purl;
		var memo1 = moment(pageInfo[i].inspectDate).format('YYYY-MM-DD HH:mm');
		var memo2 = '总体评价:'+pageInfo[i].inspectCondition.title;
		var memo3 = tempHtml;
		var memo4 = '巡检人:'+pageInfo[i].user.name;
		var memo5 = '电话:'+pageInfo[i].user.contactType;
		listView.setListViewContent(infoId,pic,memo1,memo2,memo3,memo4,memo5);
	}
	$(str).listview('refresh');
}
$(document).ready(
		function() {
			$("#goHome").attr(
					"href",
					$("#goHome").attr("href") + "&&identify="
							+ $('#identify').val());
		});
// 删除该条巡检,使用的是restful风格的DELETE提交方式
function deleteInspection(data) {

	if (confirm('确定删除该条巡检记录?')) {
		$
				.ajax({
					url : window.rootPath + '/ui/bussinessInspection/delete/'
							+ data.id,
					dataType : 'json',
					type : 'get',
					data : '_method=DELETE',
					success : function(result) {
						// 返回成功gg
						if (result.code == 1) {
							// 放回商家的巡检页面
							window.location = window.rootPath
									+ '/ui/bussinessInspection?device=mobile&identify='
									+ data.businesssellerBaseInfo.id;
						}
					}
				});
	}
};

// 修改页面
function editInspection(data) {
	if (confirm('确定修改该条巡检记录?')) {
		// 修改页面
		window.location = window.rootPath
				+ "/ui/bussinessInspection/saveOrUpdate/" + data.id
				+ "?device=mobile&identify=" + data.businesssellerBaseInfo.id;
	}
};

$("#saveInspectBtn").click(function() {

	window.location = window.rootPath
			+ "/ui/bussinessInspection/saveOrUpdate?device=mobile&identify="
			+ $("#identify").val();
	
	
});
